package com.example.service;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.crypto.SecureUtil;
import com.example.common.Constants;
import com.example.common.enums.ResultCodeEnum;
import com.example.common.enums.RoleEnum;
import com.example.entity.Account;
import com.example.entity.ZyzAdmin;
import com.example.exception.CustomException;
import com.example.mapper.ZyzAdminMapper;
import com.example.utils.TokenUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 志愿者管理员业务处理
 **/
@Service
public class ZyzAdminService {
    @Autowired
    private ZyzAdminMapper adminMapper;

    /**
     * 添加
     * @param admin
     */
    public int add(ZyzAdmin admin) {
        if (ObjectUtil.isEmpty(admin.getPassword())) {
            admin.setPassword(Constants.USER_DEFAULT_PASSWORD);
        }
        if (ObjectUtil.isEmpty(admin.getName())) {
            admin.setName(admin.getUsername());
        }
        if (ObjectUtil.isEmpty(admin.getPassword())){
            admin.setPassword("111111");
        }
        String password = admin.getPassword();
        String md5 = SecureUtil.md5(password);
        admin.setPassword(md5);

        ZyzAdmin dbAdmin = adminMapper.selectByUsername(admin.getUsername());
        if (ObjectUtil.isNotNull(dbAdmin)){
            return -1;
        }
        return adminMapper.insert(admin);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    public int deleteById(Integer id) {
        return adminMapper.deleteById(id);
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    public int deleteBatch(String ids) {
        return adminMapper.deleteByIds(Convert.toStrArray(ids));
    }

    /**
     * 修改
     * @param admin
     */
    public void updateById(ZyzAdmin admin) {
        String password = admin.getPassword();
        String md5 = SecureUtil.md5(password);
        admin.setPassword(md5);
        adminMapper.updateById(admin);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    public ZyzAdmin selectById(Integer id) {
        return adminMapper.selectById(id);
    }

    public List<ZyzAdmin> selectAll(ZyzAdmin admin) {
        return adminMapper.selectAll(admin);
    }

    public PageInfo<ZyzAdmin> selectPage(ZyzAdmin admin, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<ZyzAdmin> list = adminMapper.selectAll(admin);
        return PageInfo.of(list);
    }

    /**
     * 登录
     * @param account
     * @return
     */
    public Account login(Account account) {
        Account zyzAdmin = adminMapper.selectByUsername2(account.getUsername());
        if (!ObjectUtil.isNotNull(zyzAdmin)) {
            throw new CustomException(ResultCodeEnum.USER_NOT_EXIST_ERROR);
        }
        String password = account.getPassword();
        String md5 = SecureUtil.md5(password);
        if (!zyzAdmin.getPassword().equals(md5)){
            throw new CustomException(ResultCodeEnum.USER_ACCOUNT_ERROR);
        }
        //生成token
        String tokenData =  zyzAdmin.getId() + "-" + RoleEnum.ADMIN.name();
        String token = TokenUtils.createToken(tokenData, zyzAdmin.getPassword());
        zyzAdmin.setToken(token);
        return zyzAdmin;
    }

    public Account selectById2(Integer valueOf) {
        return adminMapper.selectById2(valueOf);
    }


}
